<%@page contentType="text/html; charset=utf-8"%>
<script>
    function closeHeader(navHeader){
        navHeader.css("display","none");
        $(".select_condition").each(function() {
              $(this).find("span").removeClass("fa-caret-up").addClass("fa-caret-down");
        })

    }

    function lazyLoad() {
        $("img.lazy").lazyload({effect: "fadeIn", container: ".content.infinite-scroll"});
    };

    function ajaxTODO(param,content,cls){
        var url = queryUrl();
//        var data = queryData();
        var ajaxParam ={
            url:url,
            type: "POST",
            dataType: "json",
            success:function(data){
                if(!!cls){
                    content.empty();
                    content.attr("data-pageNum",1);
//                    content.attr("data-totalPage",data.totalPage);
                    content.next(".infinite-scroll-preloader").css("display","block");
                }
                //第一次刷新可能会出现bug
                content.attr("data-totalPage",data.totalPage);
                addItems(data,content);

                var totalPage = content.attr("data-totalPage");
                var pageNum = content.attr("data-pageNum");
                if(parseInt(totalPage)< parseInt(pageNum)){
                    //alert("no data");
                    contentList.next('.infinite-scroll-preloader').css("display","none");
                    return;
                }
            }
        }
//        $.extend(param,data);
//        console.log(param);
        if(!!cls){
            content.attr("data-totalPage",1);
            content.attr("data-pageNum",1);
            content.next(".infinite-scroll-preloader").css("display","block");
        }
        if(!!param){
            ajaxParam.data = param;
            var totalPage = content.attr("data-totalPage");
            if(parseInt(totalPage)< parseInt(param.pageNum)){
                //alert("no data");
                contentList.next('.infinite-scroll-preloader').css("display","none");
                return;
            }
        }else{
            var totalPage = content.attr("data-totalPage");
            var pageNum = content.attr("data-pageNum");
            if(parseInt(totalPage)< parseInt(pageNum)){
                //alert("no data");
                contentList.next('.infinite-scroll-preloader').css("display","none");
                return;
            }
        }



        $.ajax(ajaxParam);

    }

    $(document).on('infinite', '.infinite-scroll',function() {

        // 如果正在加载，则退出
        if (loading) return;

        // 设置flag
        loading = true;

        setTimeout(function() {
            loading = false;

            if($(".infinite-scroll-preloader").size()==0){
                $.detachInfiniteScroll($('.infinite-scroll'));
                return;
            }

            var param = {};
            <%--var maxItems = ${cyzjPager.totalCount};--%>

            param.pageNum = contentList.attr("data-pageNum");
            ajaxTODO(param,contentList,false);
        }, 1000);
    });

    function initTab() {
        if (!!location.hash) {
            var hash = location.hash;
            var hashLink = hash+"Link";
            if(hash!="#tab1") {
                $(".tab-link.active").removeClass("active");
                $("#tab1").removeClass("active");
                $(hash).addClass("active");
                $(hashLink).addClass("active");
            }
        }
    }

    $(".tab-link").on("click",function(){
        $(".tab.active").removeClass("active");
        $(".tab-link.active").removeClass("active");
        var tab = $(this).attr("href");
        $(tab).addClass("active");
        $(this).addClass("active")
        contentList = $(".tab.active > .contentList");
        var param = {};
        param.pageNum = contentList.attr("data-pageNum");
        location.hash = $(this).attr("href");
        ajaxTODO(param,contentList,false);
    });

    $(".nav_header > div").on("click",function(){
        var $this = $(this);
        $this.siblings(".search_default").removeClass("search_default");
        $this.addClass("search_default");
        var param = {};
        param.pageNum = 1;
        ajaxTODO(param,contentList,true);
        closeHeader($this.parent());
    });


    $(function(){
        $.each($(".nav_header"),function(){
            $(this).css("display","none");
        })

        $(".select_condition").on("click",function(){
            var $select = $(this);
            var type = $select.data("type");
            $.each($("span.fa.fa-caret-up"),function(){
                $(this).removeClass("fa-caret-up").addClass("fa-caret-down");
            })
            $.each($(".nav_header"),function(){
                var $this =  $(this);
                if($this.hasClass(type)) {
                    if ($this.css("display")=="none") {
                        $this.css("display", "block");
                        $select.find("span").removeClass("fa-caret-down").addClass("fa-caret-up");
                    } else {
                        $this.css("display", "none");
                    }
                }else if($this.css("display")=="block"){
                    $this.css("display","none");
                }
            });
        });
    })
</script>